#!/usr/bin/octave -qf

arg = argv;
signal = load("-ascii", arg{1});

% pick the highest peak in the last two-thirds of the run
height = max(signal(floor(end/3):end));
quantum = height/10;
threshold = 2*quantum*abs((sort(floor(signal ./ quantum)))(floor(end/2)));

% clip the high levels in the leading garbage, and clip the low levels everywhere
outliers = find(signal > 1.5*height);
signal(outliers) = height;
outliers = find(signal < 1.5*threshold); % was 2
signal(outliers) = 0;

% randomize
signal = smoothflat(signal);
#signal = signal + 0.02*threshold*randn(size(signal));

save -ascii - signal;
